package com.rtos.model.io;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @Author: biubiu0002
 * @Date: 2021-08-30 22:23
 **/
public class IOController {
    private byte[] wBuffer;
    private byte[] rBuffer;
    private File diskFile;
    public IOController(File diskFile) {
        this.wBuffer = new byte[64];
        this.rBuffer = new byte[64];
        this.diskFile = diskFile;
    }
    public void initializeDisk() {
        byte[] bytes = new byte[64];
        try {
            FileOutputStream fos = new FileOutputStream(this.diskFile);
            for(int i = 0; i < 128; ++i) {
                fos.write(bytes, 0, 64);
            }
            fos.close();
        } catch (FileNotFoundException var3) {
            Logger.getLogger(IOController.class.getName()).log(Level.SEVERE, (String)null, var3);
        } catch (IOException var4) {
            Logger.getLogger(IOController.class.getName()).log(Level.SEVERE, (String)null, var4);
        }
    }
    public byte[] readBlockFromDisk(int blocknum){
        try{
            long position = blocknum*64;
            RandomAccessFile r = new RandomAccessFile(diskFile, "r");
            r.seek(position);
            r.read(rBuffer,0,64);
            r.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rBuffer;
    }

}
